package com.energy.analysis.weather.controller;

import com.energy.analysis.comm.base.BaseController;
import com.energy.analysis.comm.page.Page;
import com.energy.analysis.comm.utils.DateUtils;
import com.energy.analysis.comm.utils.MapAndBeanUtil;
import com.energy.analysis.comm.utils.StringUtils;
import com.energy.analysis.weather.entity.WeatherData;
import com.energy.analysis.weather.entity.WeatherDataStal;
import com.energy.analysis.weather.service.WeatherService;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(value = "/weather")
public class WeatherController extends BaseController<WeatherData> {

    @Autowired
    private WeatherService weatherService;

    @RequiresPermissions("weather_list:view")
    @RequestMapping("/pandect")
    public ModelAndView index(HttpSession session) {
        pageInitToSession(session);
        ModelAndView model = new ModelAndView("weather/weather");
        WeatherDataStal weatherDataStal = weatherService.findWeatherStalCurr();
        if (weatherDataStal == null) {
            return model;
        }
        model.addObject("weatherDataStal", weatherDataStal);
        String tm = weatherDataStal.getCrtTm();
        model.addObject("shikuang", tm.substring(0, 2) + ":" + tm.substring(2, 4));

        //风的等级
        String winLevl = "0";
        //风的名称
        String winName = "无风";
        if (null != weatherDataStal.getWinSMax()) {
            float bd = weatherDataStal.getWinSMax().floatValue();
            if (bd >= new Float(0) && bd <= new Float(0.2)) {
                winLevl = "0";
                winName = "无风";
            } else if (bd >= new Float(0.3) && bd <= new Float(1.5)) {
                winLevl = "1";
                winName = "软风";
            } else if (bd >= new Float(1.6) && bd <= new Float(3.3)) {
                winLevl = "2";
                winName = "轻风";
            } else if (bd >= new Float(3.4) && bd <= new Float(5.4)) {
                winLevl = "3";
                winName = "微风";
            } else if (bd >= new Float(5.5) && bd <= new Float(7.9)) {
                winLevl = "4";
                winName = "和风";
            } else if (bd >= new Float(8.0) && bd <= new Float(10.7)) {
                winLevl = "5";
                winName = "清风";
            } else if (bd >= new Float(10.8) && bd <= new Float(13.8)) {
                winLevl = "6";
                winName = "强风";
            } else if (bd >= new Float(13.9) && bd <= new Float(17.1)) {
                winLevl = "7";
                winName = "疾风";
            } else if (bd >= new Float(17.2) && bd <= new Float(20.7)) {
                winLevl = "8";
                winName = "大风";
            } else if (bd >= new Float(20.8) && bd <= new Float(24.4)) {
                winLevl = "9";
                winName = "烈风";
            } else if (bd >= new Float(24.5)) {
                winLevl = "10";
                winName = "狂风";
            }
        }
        BigDecimal temInt = new BigDecimal(weatherDataStal.getTem().intValue());
        model.addObject("winLevl", winLevl);
        model.addObject("winName", winName);
        BigDecimal zero = BigDecimal.ZERO;
        BigDecimal pb = new BigDecimal(40);
        BigDecimal dd = new BigDecimal(1.4);
        //以0度 像素40为标准,每增加一度就会40+0.8 [40 + (温度-zero)*0.8]
        BigDecimal xiangsu = pb.add(temInt.subtract(zero).multiply(dd));
        model.addObject("xiangsu", xiangsu.toString());
        //取整的温度
        model.addObject("wendu", temInt);


        List<WeatherData> weatherDataList = weatherService.getWeather7Day();
        if (weatherDataList != null) {
            model.addObject("data7", weatherDataList);
            //为当天天气
            for (int i = 0; i < weatherDataList.size(); i++) {
                WeatherData weatherData = weatherDataList.get(i);
                if (DateUtils.getYmdDate().equals(weatherData.getDay())) {
                    model.addObject("weatherData", weatherData);
                    //日期
                    model.addObject("dt", weatherData.getCrtDt().substring(6));
                }
            }
        }
        return model;
    }
    
    @RequiresPermissions("weather_detail:view")
    @RequestMapping("/details")
    public ModelAndView details(HttpSession session) {
        pageInitToSession(session);
        ModelAndView model = new ModelAndView("weather/weather_detail");
        //获取4天详细信息
        List<WeatherData> list = weatherService.getDay4Data();
        //4天天气信息
        WeatherData wd0 = null, wd1 = null, wd2 = null, wd3 = null;
        //4天 24小時温度json
        String hour0 = "", hour1 = "", hour2 = "", hour3 = "";
        //4天  24小時时间信息
        String h0_d = "", h1_d = "", h2_d = "", h3_d = "";
        //4天  24小時温度信息
        String h0_w = "", h1_w = "", h2_w = "", h3_w = "";

        if (list.size() > 0) {
            wd0 = list.get(0);
            hour0 = wd0.getHour3Data();
            h0_d = "";
            h0_w = "";
            List<Map<String, Object>> listHour0 = new Gson().fromJson(hour0, new TypeToken<List<Map<String, Object>>>() {
            }.getType());
            for (int i = 0; i < listHour0.size(); i++) {
                h0_d += "'" + listHour0.get(i).get("d") + "'";
                h0_w += listHour0.get(i).get("w");
                if (i < listHour0.size() - 1) {
                    h0_d += ",";
                    h0_w += ",";
                }
            }
        }
        if (list.size() > 1) {
            wd1 = list.get(1);
            hour1 = wd1.getHour3Data();
            h1_d = "";
            h1_w = "";
            List<Map<String, Object>> listHour1 = new Gson().fromJson(hour1, new TypeToken<List<Map<String, Object>>>() {
            }.getType());
            for (int i = 0; i < listHour1.size(); i++) {
                h1_d += "'" + listHour1.get(i).get("d") + "'";
                h1_w += listHour1.get(i).get("w");
                if (i < listHour1.size() - 1) {
                    h1_d += ",";
                    h1_w += ",";
                }
            }
        }
        if (list.size() > 2) {
            wd2 = list.get(2);
            hour2 = wd2.getHour3Data();
            h2_d = "";
            h2_w = "";
            List<Map<String, Object>> listHour2 = new Gson().fromJson(hour2, new TypeToken<List<Map<String, Object>>>() {
            }.getType());
            for (int i = 0; i < listHour2.size(); i++) {
                h2_d += "'" + listHour2.get(i).get("d") + "'";
                h2_w += listHour2.get(i).get("w");
                if (i < listHour2.size() - 1) {
                    h2_d += ",";
                    h2_w += ",";
                }
            }
        }
        if (list.size() > 3) {
            wd3 = list.get(3);
            hour3 = wd3.getHour3Data();
            h3_d = "";
            h3_w = "";
            List<Map<String, Object>> listHour3 = new Gson().fromJson(hour3, new TypeToken<List<Map<String, Object>>>() {
            }.getType());
            for (int i = 0; i < listHour3.size(); i++) {
                h3_d += "'" + listHour3.get(i).get("d") + "'";
                h3_w += listHour3.get(i).get("w");
                if (i < listHour3.size() - 1) {
                    h3_d += ",";
                    h3_w += ",";
                }
            }
        }
        model.addObject("wd0", wd0);
        model.addObject("wd1", wd1);
        model.addObject("wd2", wd2);
        model.addObject("wd3", wd3);
        model.addObject("h0_d", h0_d);
        model.addObject("h0_w", h0_w);
        model.addObject("h1_d", h1_d);
        model.addObject("h1_w", h1_w);
        model.addObject("h2_d", h2_d);
        model.addObject("h2_w", h2_w);
        model.addObject("h3_d", h3_d);
        model.addObject("h3_w", h3_w);
        return model;
    }
    
    @RequiresPermissions("weather_situation:view")
    @RequestMapping("/situation")
    public ModelAndView situation(HttpSession session) {
        pageInitToSession(session);
        ModelAndView model = new ModelAndView("weather/weather_situation");
        return model;
    }

    @RequiresPermissions("weather_situation:view")
    @RequestMapping("/situation/list")
    @ResponseBody
    public Page<WeatherData> list(HttpServletRequest request, HttpServletResponse response, Page<WeatherData> page) {
        initPage(page);
        String beginTime = (String) page.getParams().get("beginTime");
        String endTime = (String) page.getParams().get("endTime");
        if (StringUtils.isNotEmpty(beginTime)) {
            page.getParams().put("beginTime", beginTime.replaceAll("-", ""));
        }
        if (StringUtils.isNotEmpty(endTime)) {
            page.getParams().put("endTime", endTime.replaceAll("-", ""));
        }
        weatherService.getAvgTemList(page);
        weatherService.getAvgTemListCount(page);
        return page;
    }

    @RequiresPermissions("weather_situation:view")
    @RequestMapping("/situation/list/export")
    public void listExport(String beginTime, String endTime, HttpServletRequest request, HttpServletResponse response, Page<WeatherData> page) {
        if (StringUtils.isNotEmpty(beginTime)) {
            page.getParams().put("beginTime", beginTime.replaceAll("-", ""));
        }
        if (StringUtils.isNotEmpty(endTime)) {
            page.getParams().put("endTime", endTime.replaceAll("-", ""));
        }
        page.setLimit(9999999);//因为需要下载全部页数据
        page.setOffset(0);
        weatherService.getAvgTemList(page);
        List<WeatherData> list = page.getRows();
        List<Map<String, Object>> retList = MapAndBeanUtil.objectsToMaps(list);
        for (Map<String, Object> map : retList) {
            String day = (String) map.get("day");
            if (StringUtils.isNotEmpty(day))
                map.put("day", day.substring(0, 4) + "-" + day.substring(4, 6) + "-" + day.substring(6));
            float teA = (float) map.get("teA");
            map.put("teA", (int) teA);
            String teWd1 = (String) map.get("teWd1");
            String teWe1 = (String) map.get("teWe1");
            String teWd2 = (String) map.get("teWd2");
            String teWe2 = (String) map.get("teWe2");
            String baiWd = "", wanWd = "";
            if (StringUtils.isNotEmpty(teWd1)) baiWd = teWd1 + " ";
            if (StringUtils.isNotEmpty(teWe1)) baiWd += teWe1;
            map.put("baiWd", baiWd);
            if (StringUtils.isNotEmpty(teWd2)) wanWd = teWd2 + " ";
            if (StringUtils.isNotEmpty(teWe2)) wanWd += teWe2;
            map.put("wanWd", wanWd);
        }
        String fileName = "天气情况" + DateUtils.getYmdHmsDate() + ".xls";
        exportFile(response, retList, "weather_situation", fileName, null);
    }

    @RequiresPermissions("weather_situation:view")
    @RequestMapping("/situation/day/hour/weather")
    @ResponseBody
    public List<Map<String, Object>> dayHourWeather(String day) {
        if (StringUtils.isNotEmpty(day)) {
            return weatherService.getStalWeatherByDay(day);
        }
        return null;
    }

}
